﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCEdu;
using System.Data;
using System.Data.SqlClient;
using Utils;

public partial class Search_Contract : System.Web.UI.Page
{
    DataSet dataSet;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) { return; }
        
        SqlConnection sqlCon = null;

        try
        {
            sqlCon = new SqlConnection(Common.Conn);
            SqlCommand cmd = new SqlCommand("ContractHistory_Search", sqlCon);
            cmd.CommandType = CommandType.StoredProcedure;
            sqlCon.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);

            GridView1.DataSource = dataSet;
            GridView1.DataBind();

            cboContractNumber.DataSource = dataSet;
            cboCusName.DataSource = dataSet.Tables[0].DefaultView.ToTable(true, "CustomerName");
            cboTutorName.DataSource = dataSet.Tables[0].DefaultView.ToTable(true, "TutorName");
            cboStatus.DataSource = dataSet.Tables[0].DefaultView.ToTable(true, "Status");

            cboContractNumber.DataBind();
            cboCusName.DataBind();
            cboTutorName.DataBind();
            cboStatus.DataBind();

        }
        catch (Exception ex)
        {
            MyMessage.ShowMessage("Lỗi kết nối với cơ sở dữ liệu");
            Response.Write(ex.ToString());
        }
        finally
        {
            if (sqlCon != null) sqlCon.Close();
        }

       
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection sqlCon = null;
        dataSet = null;
        try
        {
            sqlCon = new SqlConnection(Common.Conn);
            SqlCommand cmd = new SqlCommand("ContractHistory_Search", sqlCon);
            cmd.CommandType = CommandType.StoredProcedure;

            if (cboContractNumber.SelectedText.Trim() != "")
            {
                cmd.Parameters.Add(new SqlParameter("@ContractNumber", cboContractNumber.SelectedText.Trim()));
            }

            cmd.Parameters.Add(new SqlParameter("@CustomerName", cboCusName.SelectedText.Trim()));
            cmd.Parameters.Add(new SqlParameter("@TutorName", cboTutorName.SelectedText.Trim()));
            cmd.Parameters.Add(new SqlParameter("@ContractDate_Start", tbxContractDate_Start.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@ContractDate_End", tbxContractDate_End.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@ContractPrice_Start", tbxPrice_Start.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@ContractPrice_End", tbxPrice_End.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Subjects", tbxSubjects.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Status", cboStatus.SelectedText.Trim()));

            sqlCon.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            dataSet = new DataSet();
            adapter.Fill(dataSet);

            GridView1.DataSource = dataSet;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            MyMessage.ShowMessage("Lỗi kết nối với cơ sở dữ liệu");
            Response.Write(ex.ToString());
        }
        finally
        {
            if (sqlCon != null) sqlCon.Close();
        }

    }
}